home *** CD-ROM | disk | FTP | other *** search
/ Mac-Source 1994 July / Mac-Source_July_1994.iso / Other Langs / Tickle-4.0 (tcl) / tcl / expecTerm / pty_usg.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-11-17  |  3.4 KB  |  161 lines  |  [TEXT/MPS ]

  1. /* pty_usg.c - routines to allocate ptys - usg version
  2.  
  3. Written by: Don Libes, NIST, 2/6/90
  4.  
  5. Design and implementation of this program was paid for by U.S. tax
  6. dollars.  Therefore it is public domain.  However, the author and NIST
  7. would appreciate credit if this program or parts of it are used.
  8.  
  9. */
  10.  
  11. #include <unistd.h>
  12. #include <sys/types.h>
  13. #include <sys/stat.h>
  14. #include <sys/ioctl.h>
  15. #include <sys/file.h>
  16. #ifdef AUX2
  17. #include <fcntl.h>
  18. #else
  19. #include <sys/fcntl.h>
  20. #endif
  21. #include "term.h"
  22. #include "translate.h"
  23.  
  24. void debuglog();
  25.  
  26. #ifndef TRUE
  27. #define TRUE 1
  28. #define FALSE 0
  29. #endif
  30. #ifdef UTS
  31. #include <sys/vty.h>
  32. static char    line[MAXPTYNAMELEN];
  33. static char    sline[MAXPTYNAMELEN];
  34. #else
  35. static char    line[] = "/dev/ptyXX";
  36. #endif
  37. static char    *tty_type;        /* ptr to char [pt] denoting
  38.                        whether it is a pty or tty */
  39. static char    *tty_bank;        /* ptr to char [p-z] denoting
  40.                        which bank it is */
  41. static char    *tty_num;        /* ptr to char [0-f] denoting
  42.                        which number it is */
  43.  
  44. static void
  45. pty_stty(s,name)
  46. char *s;        /* args to stty */
  47. char *name;        /* name of pty */
  48. {
  49. #define MAX_ARGLIST 10240
  50.     char buf[MAX_ARGLIST];    /* overkill is easier */
  51.  
  52.     sprintf(buf,"stty %s < %s > %s",s,name,name);
  53.     system(buf);
  54. }
  55.  
  56. struct    termio exp_tty_original;
  57.  
  58. #define GET_TTYTYPE    0
  59. #define SET_TTYTYPE    1
  60. static void
  61. ttytype(request,fd,s)
  62. int request;
  63. int fd;
  64. char *s;    /* stty args, used only if request == SET_TTYTYPE */
  65. {
  66.     static int is_a_tty;
  67.  
  68.     if (request == GET_TTYTYPE) {
  69.         if (-1 == ioctl(fd, TCGETA, (char *)&exp_tty_original)) {
  70.             is_a_tty = FALSE;
  71.         } else is_a_tty = TRUE;
  72.     } else {    /* type == SET_TTYTYPE */
  73.         if (is_a_tty) {
  74.             (void) ioctl(fd, TCSETA, (char *)&exp_tty_original);
  75.         } else {
  76.             /* if running in the background, we have no access */
  77.             /* to a a tty to copy parameters from, so use ones */
  78.             /* supplied by original Makefile */
  79.             debuglog("getptyslave: (default) stty %s\n",DFLT_STTY);
  80.             pty_stty(DFLT_STTY,line);
  81.         }
  82.         if (s) {
  83.             /* give user a chance to override any terminal parms */
  84.             debuglog("getptyslave: (user-requested) stty %s\n",s);
  85.             pty_stty(s,line);
  86.         }
  87.     }
  88. }
  89.  
  90. void
  91. init_pty()
  92. {
  93.     tty_type = &line[strlen("/dev/")];
  94.     tty_bank = &line[strlen("/dev/pty")];
  95.     tty_num  = &line[strlen("/dev/ptyp")];
  96.     ttytype(GET_TTYTYPE,0,(char *)0);
  97. }
  98.  
  99. #ifndef R_OK
  100. /* 3b2 doesn't define these according to jthomas@nmsu.edu. */
  101. #define R_OK 04
  102. #define W_OK 02
  103. #endif
  104.  
  105. /* returns fd of master end of pseudotty */
  106. int
  107. getptymaster()
  108. {
  109.     char *hex;
  110.     struct stat stat_buf;
  111.     int master;
  112. #ifdef UTS
  113.     master = getpty(line, sline, O_RDWR);
  114. #else
  115.     for (*tty_bank = 'p';; (*tty_bank)++) {
  116.         *tty_num = '0';
  117.         if (stat(line, &stat_buf) < 0) break;
  118.         for (hex = "0123456789abcdef";*hex;hex++) {
  119.             *tty_num = *hex;
  120.             *tty_type = 'p';
  121.             if (0 <= (master = open(line, O_RDWR))) {
  122. #endif
  123.                 /* verify slave side is usable */
  124.                 *tty_type = 't';
  125.                 if (access(line, R_OK|W_OK) != 0) {
  126.                     (void) close(master);
  127. #ifdef UTS                
  128.                     return(-1);
  129. #else
  130.                     continue;
  131. #endif
  132.                 }
  133.                 return(master);
  134. #ifndef UTS
  135.             }
  136.         }
  137.     }
  138. #endif
  139.     return(-1);
  140. }
  141.  
  142. int
  143. getptyslave(stty_args)
  144. char *stty_args;
  145. {
  146.     int slave;
  147. #ifdef UTS
  148.     if (0 > (slave = open(sline, O_RDWR))) return(-1);
  149. #else
  150.     if (0 > (slave = open(line, O_RDWR))) return(-1);
  151. #endif
  152.  
  153.     /* sanity check - if slave not 0, skip rest of this and return */
  154.     /* to what will later be detected as an error in caller */
  155.     if (0 != slave) return(slave);
  156.  
  157.     fcntl(0,F_DUPFD,1);    /* duplicate 0 onto 1 to prepare for stty */
  158.     ttytype(SET_TTYTYPE,slave,stty_args);
  159.     return(slave);
  160. }
  161.